package com.helloworldagd;

import android.view.MotionEvent;

import com.ladydinde.R;
import com.turboconnard.core.Agd;
import com.turboconnard.display.Cube;
import com.turboconnard.display.Sprite;
import com.turboconnard.events.AccelerometerEvent;
import com.turboconnard.events.Event;
import com.turboconnard.geom.Point3D;
import com.turboconnard.hardware.Accelerometer;
import com.turboconnard.hardware.Vibrato;

public class HelloWorldAGD extends Agd {
	
	private Sprite s;
	private float tx;
	private float ty;
	public HelloWorldAGD(){
		super();	
		
		Accelerometer.getInstance().addEventListener(AccelerometerEvent.ON_CHANGE, this);
		tx = ty = 0;
		s = new Sprite(); 
		s.x = stage.width / 2;
		s.y = stage.height / 2;
		stage.addChild(s);	 
	}
	public void addCube(){
		Cube c = new Cube((float) (Math.random()*80 + 20));
		c.texture = R.drawable.test;
		c.x = (float) Math.random()*stage.width - (stage.width/2);
		c.y = (float) Math.random()*stage.height-(stage.height/2);
		c.z = (float) Math.random()*stage.height-(stage.height/2);
		s.addChild(c);		

		Vibrato.getInstance().vibrate(30);
	} 
	public boolean onTouchEvent(MotionEvent e){
		if(e.getAction() == MotionEvent.ACTION_DOWN){
			addCube();
		}
		return true;
	}
	public void update(){
			
	}
	public void event(Event e){
		if(e.name == AccelerometerEvent.ON_CHANGE){
			Point3D p = (Point3D) e.param;			
			tx += (p.x - tx)/5;
			ty += (p.y - ty)/5;
			
			s.rotationX += tx;
			s.rotationY += ty;
		}		
	}
}
